const app = getApp()
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
Page({
  data: {
    page: 1,
    limit: 10,
    dataList: [],
    noData: true,
    bindingCol: true,
    lastTime: 0,
    status: 1,
    dataListCol: true,
    codeDemo: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCouponsList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    wx.showLoading({
      title: '刷新中',
    })
    this.getCouponsList();
    wx.hideLoading()

    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = this.data.page;
    var total_page = this.data.total_page;

    if (total_page > page) {
      this.setData({
        page: page + 1
      })
      wx.showLoading({
        title: '加载中',
      })
      this.getCouponsList();
    } else {
      this.setData({
        noData: false
      })
    }
  },
  getCouponsList: function () {
    var params = new Object()
    var that = this;
    params.page = this.data.page;
    params.limit = this.data.limit;
    if (this.data.status != undefined) {
      params.status = this.data.status;
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'lotteryCard/getList',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {
          if (res.data.data.paging.page == 1) {
            that.data.dataList = [];
          }
          var dataList = that.data.dataList.concat(res.data.data.list);
          var is_show_hint = res.data.data.is_show_hint;
          // console.log(is_show_hint);
          var bindingCol
          if (is_show_hint == 1) {
            bindingCol = false
          } else {
            bindingCol = true
          }
          if (dataList.length > 0) {
            var dataListCol = true
          } else {
            var dataListCol = false
          }
          that.setData({
            dataList: dataList,
            total_page: res.data.data.paging.total_page,
            bindingCol: bindingCol,
            is_show_bind: res.data.data.is_show_bind,
            dataListCol: dataListCol
          })
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
        wx.stopPullDownRefresh();
        wx.hideLoading()
      }
    })
  },
  selectCid: function (e) {
    // console.log(e);
    var status = e.currentTarget.dataset.status;
    this.setData({
      page: 1,
      status: status,
      noData: true
    })
    // wx.showLoading({
    //   title: '加载中',
    // })
    this.getCouponsList();
  },
  useCoupons:function(e){
    var id = e.currentTarget.dataset.id;
    var params = new Object()
    var that = this;
    params.id = id
    api.POST({
      params: params,
      url: app.globalData.base_url + 'lotteryCard/getQrcode',
      success: function (res) {
        console.log(res);
        if (util.isEmpty(res.data.code)) {
          var code_img = res.data.data.img_url;
          console.log(code_img);
          that.setData({
            codeDemo: false,
            code_img: code_img
          })
        }else{
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })

  },
  closeCodeDemo: function () {
    this.setData({
      codeDemo: true
    })
  },
})